package com.woniu.microcommunityserver.activity.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.activity.entity.ActivityManagementInfoEntity;
import com.woniu.microcommunityserver.activity.service.ActivityManagementInfoService;
import com.woniu.microcommunityserver.res.ResponseMsg;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("activity")
public class ActivityManagementInfoController {
    @Resource
    private ActivityManagementInfoService activityManagementInfoService;

    /**
     * 查询所有活动
     *
     * @param pageNum
     * @return
     */

    @GetMapping("findAll")
    public ResponseMsg findAllActivity(int pageNum) {
        PageHelper.startPage(pageNum, 10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<ActivityManagementInfoEntity> activities = activityManagementInfoService.findAllActivity();
        PageInfo<ActivityManagementInfoEntity> pageInfo = new PageInfo<ActivityManagementInfoEntity>(activities);
        return ResponseMsg.builder().code(200).msg("所有活动查询成功！").data(pageInfo).build();
    }


    @GetMapping("findAny")
    public ResponseMsg findByAny(int pageNum,ActivityManagementInfoEntity activityManagementInfoEntity) throws ParseException {
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        Map<String,Object> map =new HashMap<>();

        if(activityManagementInfoEntity.getActivityTitle()==""){
            activityManagementInfoEntity.setActivityTitle(null);
        }

        if(activityManagementInfoEntity.getVillageName()==""){
            activityManagementInfoEntity.setVillageName(null);
        }

        Date time1 =new Date(activityManagementInfoEntity.getActivityStartTime().toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeFormat1 = sdf.format(time1);
        Date time2 =new Date(activityManagementInfoEntity.getActivityEndTime().toString());
        String timeFormat2 = sdf.format(time2);
        map.put("activityTitle",activityManagementInfoEntity.getActivityTitle());
        map.put("villageName",activityManagementInfoEntity.getVillageName());
        map.put("activityState",activityManagementInfoEntity.getActivityState());
        map.put("activityStartTime",timeFormat1);
        map.put("activityEndTime",timeFormat2);
        System.out.println(activityManagementInfoEntity);
        List<ActivityManagementInfoEntity> activity = activityManagementInfoService.findByAny(map);
        PageInfo<ActivityManagementInfoEntity> pageInfo = new PageInfo<ActivityManagementInfoEntity>(activity);
        return ResponseMsg.builder().code(200).msg("指定活动查询成功！").data(pageInfo).build();
    }

    /**
     * 通过id删除指定活动
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public ResponseMsg deleteActivity(Integer id){
        boolean flag = activityManagementInfoService.deleteActivity(id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("删除成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("删除失败！").build();
        }
    }

    @PostMapping(value = "insert")
    public ResponseMsg insertActivity(@RequestBody ActivityManagementInfoEntity activityManagementInfoEntity){
        activityManagementInfoEntity.setActivityState(0);
        boolean flag = activityManagementInfoService.insertActivity(activityManagementInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("新增成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("新增失败！").build();
        }
    }

    @PostMapping(value = "update")
    public ResponseMsg updateActivity(@RequestBody ActivityManagementInfoEntity activityManagementInfoEntity){
        if(activityManagementInfoEntity.getActivityCoverStorageAddress()==""){
            activityManagementInfoEntity.setActivityCoverStorageAddress(null);
        }
        System.out.println(activityManagementInfoEntity);
        boolean flag=activityManagementInfoService.updateActivity(activityManagementInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }


    @GetMapping("test")
    public ResponseMsg fff(ActivityManagementInfoEntity activityManagementInfoEntity){
        System.out.println(activityManagementInfoEntity.getActivityStartTime());
        return ResponseMsg.builder().code(200).msg("修改成功！").build();
    }
}
